package design.mode.effective_java.chapter7.item42;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

/**
 * @author ZhongMingde on 2021/8/28
 */

public class Test1 {
    // 匿名类实例作为函数对象-过时了!
    List list = new ArrayList();

    public void test1() {
        Collections.sort(list, new Comparator<String>() {
            @Override
            public int compare(String o1, String o2) {
                return Integer.compare(o1.length(), o2.length());
            }
        });
    }

    // lambda expression as function object (replaces anonymous class)

    public void test2() {
//        Collections.sort(list,(s1, s2) -> Integer.compare(s1.length(), s2.length()));
        Collections.sort(list, Comparator.comparingInt(String::length));
        list.sort(Comparator.comparingInt(String::length));
    }
}
